var lunboImgesUrl = "";
var chooseUserList = [];
initProject(0);

function initProject(pageIndex) {
    $.ajax({
        url: prefixUrl + "project/queryList",
        type: "post",
        data: JSON.stringify({pageIndex:pageIndex}),
        contentType: "application/json;",
        success: function (data) {
            var projectHtml = "";
            var list = data.data.rows;
            for(var i=0; i<list.length; i++){
                projectHtml = projectHtml
                    +'<option style="text-align: center;" value="'
                    +list[i].projectId
                    +'">'
                    +list[i].projectName
                    +'</option>';
            }
            $("#projectList").html(projectHtml);
            queryProUserList($("#projectList").val());
        }
    })
}

$("#projectList").change(function () {
    queryProUserList($("#projectList").val());
})

function queryProUserList(projectId) {
    $.ajax({
        url :   prefixUrl + "user/getProUserList",
        type:   "post",
        data:JSON.stringify({
            projectId:projectId
        }),
        contentType:"application/json;",
        success:function(data){
            goToLogin(data);
            console.log(data);
            var list = data.data;
            var userHtml  = '';
            for(var i=0; i<list.length; i++){
                userHtml = userHtml
                    +'<input id="'
                    +list[i].userId
                    +'" name="user" class="iptradio" type="checkbox" value="'
                    +list[i].userId + "," + list[i].userName
                    +'"">'
                    + list[i].userName;
            }
            $("#userList").html(userHtml);

            getNumberList(data);
        }
    });
}


function saveCharts() {
    if($("#noticeTitle").val() == null ||　$("#noticeTitle").val() == ""){
        alert("标题不可为空");
        return;
    }
    getUserList();
    if(chooseUserList == null || chooseUserList == ""){
        alert("人员不可为空");
        return;
    }
    if($("#noticeContent").val() == null ||　$("#noticeContent").val() == ""){
        alert("内容不可为空");
        return;
    }
    var data = {
        noticeTitle : $("#noticeTitle").val(),
        noticeContent : $("#noticeContent").val(),
        fileUrl : lunboImgesUrl,
        projectId:$("#projectList").val(),
        userList : chooseUserList
    };
    $.ajax({
        url :   prefixUrl + "notice/addnotice",
        type:   "post",
        data:JSON.stringify(data),
        contentType:"application/json;",
        success:function(data){
            goToLogin(data);
            console.log(data);
            alert(data.msg);
        }
    });
}

function getUserList(){
    var obj = document.getElementsByName("user");
    var i = 0;
    for(k in obj){
        if(obj[k].checked){
            var user = obj[k].value.split(",");
            var userInfo = {
                userId : user[0],
                userName : user[1]
            }
            chooseUserList[i] = userInfo;
            i++;
        }
    }
}
